<div class="layui-fluid layui-anim febs-anim" id="febs-price" lay-title="资费信息">
    <div class="layui-row febs-container">
        <div class="layui-col-md12">
            <div class="layui-card">
                <div class="layui-card-body febs-table-full">
                    <form class="layui-form layui-table-form" lay-filter="price-table-form">
                        <div class="layui-row">
                            <div class="layui-col-md10">
                                <div class="layui-form-item">
                                    <div class="layui-inline">
                                        <label class="layui-form-label layui-form-label-sm">资费日期</label>
                                        <div class="layui-input-inline">
                                            <input type="text" name="createtime" id="price-createtime"
                                                   autocomplete="off" class="layui-input">
                                        </div>
                                    </div>
                                    <div class="layui-inline">
                                        <label class="layui-form-label layui-form-label-sm">资费类型</label>
                                        <div class="layui-input-inline">
                                            <select name="type">
                                                <option value=""></option>
                                                <option value="0">支出</option>
                                                <option value="1">收入</option>
                                                <option value="2">退款</option>
                                            </select>
                                        </div>
                                    </div>
                                </div>
                            </div>
                            <div class="layui-col-md2 layui-col-sm12 layui-col-xs12 table-action-area">
                                <div class="layui-btn layui-btn-sm layui-btn-primary table-action" id="query">
                                    <i class="layui-icon">&#xe848;</i>
                                </div>
                                <div class="layui-btn layui-btn-sm layui-btn-primary table-action" id="reset">
                                    <i class="layui-icon">&#xe79b;</i>
                                </div>
                                <div class="layui-btn layui-btn-sm layui-btn-primary table-action action-more">
                                    <i class="layui-icon">&#xe875;</i>
                                </div>
                            </div>
                        </div>
                    </form>
                    <table lay-filter="priceTable" lay-data="{id: 'priceTable'}"></table>
                </div>
            </div>
        </div>
    </div>
</div>
<script type="text/html" id="price-type">
    {{#
    var type = {
    2: {title: '已退款', color: 'blue'},
    1: {title: '充值', color: 'green'},
    0: {title: '支出', color: 'volcano'},
    }[d.type];
    }}
    <span class="layui-badge febs-tag-{{type.color}}">{{ type.title }}</span>
</script>
<script type="text/html" id="price-option">
    <a lay-event="del"><i class="layui-icon febs-edit-area febs-red">&#xe7f9;</i></a>
</script>
<script data-th-inline="none" type="text/javascript">
    layui.use(['laydate', 'dropdown', 'jquery', 'form', 'table', 'febs'], function () {
        var $ = layui.jquery,
            laydate = layui.laydate,
            febs = layui.febs,
            form = layui.form,
            table = layui.table,
            dropdown = layui.dropdown,
            $view = $('#febs-price'),
            $query = $view.find('#query'),
            $reset = $view.find('#reset'),
            $searchForm = $view.find('form'),
            sortObject = {field: 'payment', type: 'asc'},
            tableIns;

        form.render();

        initTable();

        laydate.render({
            elem: '#price-createtime',
            type: 'date'
        });

        dropdown.render({
            elem: $view.find('.action-more'),
            click: function (name, elem, event) {
                var checkStatus = table.checkStatus('priceTable');
                if (name === 'delete') {
                    if (!checkStatus.data.length) {
                        febs.alert.warn('请选择需要删除的记录');
                    } else {
                        febs.modal.confirm('删除信息', '确定删除该条？', function () {
                            var priceIds = [];
                            layui.each(checkStatus.data, function (key, item) {
                                priceIds.push(item.id)
                            });
                            deleteprices(priceIds.join(','));
                        });
                    }
                }
            },
            options: [{
                name: 'delete',
                title: '删除信息',
            }]
        });

        table.on('tool(priceTable)', function (obj) {
            var data = obj.data,
                layEvent = obj.event;
            if (layEvent === 'del') {
                febs.modal.confirm('支付信息', '确定删除该支付信息？', function () {
                    deleteprices(data.id);
                });
            }
        });


        table.on('sort(priceTable)', function (obj) {
            sortObject = obj;
            tableIns.reload({
                initSort: obj,
                where: $.extend(getQueryParams(), {
                    field: obj.field,
                    order: obj.type
                })
            });
        });

        $query.on('click', function () {
            var params = $.extend(getQueryParams(), {field: sortObject.field, order: sortObject.type});
            tableIns.reload({where: params, page: {curr: 1}});
        });

        $reset.on('click', function () {
            $searchForm[0].reset();
            tableIns.reload({where: getQueryParams(), page: {curr: 1}, initSort: sortObject});
        });

        function initTable() {
            tableIns = febs.table.buy({
                elem: $view.find('table'),
                id: 'priceTable',
                url: ctx + 'price/list',
                cols: [[
                    {type: 'checkbox'},
                    {title: '用户名称', field: 'username'},
                    {title: '金额', field: 'payment', sort: true},
                    {title: '日期', field: 'createtime'},
                    {title: '资金类型', templet: '#price-type'},
                    {title: '操作', toolbar: '#price-option', minWidth: 100}
                ]]
            }, currentUser.username);
        }

        function getQueryParams() {
            return {
                createtime: $searchForm.find('input[name="createtime"]').val().trim(),
                type: $searchForm.find('select[name="type"]').val(),
            };
        }

        function deleteprices(priceIds) {
            febs.post(ctx + 'price/delete/' + priceIds, null, function () {
                febs.alert.success('删除成功');
                $query.click();
            });
        }
    })
</script>